package com.util;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

//博客资料
//https://blog.csdn.net/weixin_72764731/article/details/131614442

/**
 * @Description:
 * @Auther: tz
 * @Date: 2023/9/25 10:55
 */
public class JavaMailUtils {
    private JavaMailUtils() {
    }

    public static Session createSession() {
        Properties pros=new Properties();
//        pros.put("mail.smtp.host", "smtp.asiaunicom.com");465
        pros.put("mail.smtp.host", "smtp.qq.com");
        pros.put("mail.smtp.port", "25");
        pros.put("mail.smtp.auth", "true");
        pros.put("mail.smtp.starttls.enable","true" );

        //创建Session
        Session session=Session.getInstance(pros, new Authenticator() {
            String userName="422650321@qq.com";
            String password="carvkfnublrobjgj";
//            String userName="LAU_Mobile@asiaunicom.com";
//            String password="Lau20231201@";
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        });
        session.setDebug(true);
        return session;
    }

    public static void main(String[] args) {
        //1.创建Session
        try {
            Session session= JavaMailUtils.createSession();
            System.out.println(session);

            //2.创建邮件对象
            MimeMessage message=new MimeMessage(session);
            //设置邮件主题
            message.setSubject("这是一个测试邮件");
            //设置邮件内容
            message.setText("测试邮件");
            //设置发件人
            message.setFrom(new InternetAddress("422650321@qq.com"));
//            message.setFrom(new InternetAddress("LAU_Mobile@asiaunicom.com"));
            //设置收件人
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("LAU_Mobile@asiaunicom.com"));

            //3.发送邮件
            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
